/*
 * Copyright 2008-2009 Sanjay Dwivedi.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.isdinvestments.cam.infrastructure.repository;

import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.isdinvestments.cam.domain.model.Account;
import com.isdinvestments.cam.domain.model.Asset;
import com.isdinvestments.cam.domain.model.AssetClassification;
import com.isdinvestments.cam.domain.model.AssetSubClassification;
import com.isdinvestments.cam.domain.model.Customer;
import com.isdinvestments.cam.domain.model.Event;
import com.isdinvestments.cam.domain.model.EventSubType;
import com.isdinvestments.cam.domain.model.EventType;
import com.isdinvestments.cam.domain.model.Portfolio;
import com.isdinvestments.cam.domain.model.Position;
import com.isdinvestments.cam.domain.model.PositionSnapshot;
import com.isdinvestments.cam.domain.model.PositionSnapshotPK;
import com.isdinvestments.cam.domain.model.PositionTransaction;
import com.isdinvestments.cam.domain.model.SubAccount;
import com.isdinvestments.cam.domain.model.SubAccountPK;
import com.isdinvestments.common.repository.BaseJPARepository;

import org.springframework.stereotype.Repository;

/**
 * The JPA Repository implementation for Portfolio.
 */
@Repository("portfolioRepository")
public class PortfolioRepositoryJPAImpl extends BaseJPARepository implements PortfolioRepository {

	public PortfolioRepositoryJPAImpl() {
		super();
	}
	/**
	 * Find an entity by its id (primary key).
	 * @return The found entity instance or null if the entity does not exist.
	 */
	public Portfolio findPortfolioById(long id) {
		return (Portfolio)entityManager.find(Portfolio.class, new Long(id));
	}
	/**
	 * Return all persistent instances of the <code>Portfolio</code> entity.
	 */
	@SuppressWarnings("unchecked")
	public List<Portfolio> findAllPortfolios() {
        try {
    		String jpqlString = "select portfolio from " + Portfolio.class.getSimpleName() + " portfolio";
            Query query = entityManager.createQuery( jpqlString );
            return query.getResultList();
        }
        finally {
            if (entityManager != null) {
                entityManager.close();
            }
        }
	}
	/**
	 * Make the given instance managed and persistent.
	 */
	public void persistPortfolio(Portfolio portfolio) {
		entityManager.persist(entityManager.merge(portfolio));
	}
	/**
	 * Remove the given persistent instance.
	 */
	public void removePortfolio(Portfolio portfolio) {
		/*In JPA, objects detach automatically when they are serialized or when a persistence context ends.
		 The merge method returns a managed copy of the given detached entity.*/
		entityManager.remove(entityManager.merge(portfolio));
	}

	/**
	 * Find an entity by its id (primary key).
	 * @return The found entity instance or null if the entity does not exist.
	 */
	public Position findPositionById(long id) {
		return (Position)entityManager.find(Position.class, new Long(id));
	}
	/**
	 * Return all persistent instances of the <code>Position</code> entity.
	 */
	@SuppressWarnings("unchecked")
	public List<Position> findAllPositions() {
        try {
    		String jpqlString = "select position from " + Position.class.getSimpleName() + " position";
            Query query = entityManager.createQuery( jpqlString );
            return query.getResultList();
        }
        finally {
            if (entityManager != null) {
                entityManager.close();
            }
        }
	}
	/**
	 * Make the given instance managed and persistent.
	 */
	public void persistPosition(Position position) {
		entityManager.persist(entityManager.merge(position));
	}
	/**
	 * Remove the given persistent instance.
	 */
	public void removePosition(Position position) {
		/*In JPA, objects detach automatically when they are serialized or when a persistence context ends.
		 The merge method returns a managed copy of the given detached entity.*/
		entityManager.remove(entityManager.merge(position));
	}

	/**
	 * Find an entity by its id (primary key).
	 * @return The found entity instance or null if the entity does not exist.
	 */
	public PositionSnapshot findPositionSnapshotById(com.isdinvestments.cam.domain.model.PositionSnapshotPK id) {
		return (PositionSnapshot)entityManager.find(PositionSnapshot.class, id);
	}
	/**
	 * Return all persistent instances of the <code>PositionSnapshot</code> entity.
	 */
	@SuppressWarnings("unchecked")
	public List<PositionSnapshot> findAllPositionSnapshots() {
        try {
    		String jpqlString = "select positionSnapshot from " + PositionSnapshot.class.getSimpleName() + " positionSnapshot";
            Query query = entityManager.createQuery( jpqlString );
            return query.getResultList();
        }
        finally {
            if (entityManager != null) {
                entityManager.close();
            }
        }
	}
	/**
	 * Make the given instance managed and persistent.
	 */
	public void persistPositionSnapshot(PositionSnapshot positionSnapshot) {
		entityManager.persist(entityManager.merge(positionSnapshot));
	}
	/**
	 * Remove the given persistent instance.
	 */
	public void removePositionSnapshot(PositionSnapshot positionSnapshot) {
		/*In JPA, objects detach automatically when they are serialized or when a persistence context ends.
		 The merge method returns a managed copy of the given detached entity.*/
		entityManager.remove(entityManager.merge(positionSnapshot));
	}

	/**
	 * Find an entity by its id (primary key).
	 * @return The found entity instance or null if the entity does not exist.
	 */
	public PositionSnapshotPK findPositionSnapshotPKById(long id) {
		return (PositionSnapshotPK)entityManager.find(PositionSnapshotPK.class, new Long(id));
	}
	/**
	 * Return all persistent instances of the <code>PositionSnapshotPK</code> entity.
	 */
	@SuppressWarnings("unchecked")
	public List<PositionSnapshotPK> findAllPositionSnapshotPKs() {
        try {
    		String jpqlString = "select positionSnapshotPK from " + PositionSnapshotPK.class.getSimpleName() + " positionSnapshotPK";
            Query query = entityManager.createQuery( jpqlString );
            return query.getResultList();
        }
        finally {
            if (entityManager != null) {
                entityManager.close();
            }
        }
	}
	/**
	 * Make the given instance managed and persistent.
	 */
	public void persistPositionSnapshotPK(PositionSnapshotPK positionSnapshotPK) {
		entityManager.persist(entityManager.merge(positionSnapshotPK));
	}
	/**
	 * Remove the given persistent instance.
	 */
	public void removePositionSnapshotPK(PositionSnapshotPK positionSnapshotPK) {
		/*In JPA, objects detach automatically when they are serialized or when a persistence context ends.
		 The merge method returns a managed copy of the given detached entity.*/
		entityManager.remove(entityManager.merge(positionSnapshotPK));
	}

	/**
	 * Find an entity by its id (primary key).
	 * @return The found entity instance or null if the entity does not exist.
	 */
	public PositionTransaction findPositionTransactionById(long id) {
		return (PositionTransaction)entityManager.find(PositionTransaction.class, new Long(id));
	}
	/**
	 * Return all persistent instances of the <code>PositionTransaction</code> entity.
	 */
	@SuppressWarnings("unchecked")
	public List<PositionTransaction> findAllPositionTransactions() {
        try {
    		String jpqlString = "select positionTransaction from " + PositionTransaction.class.getSimpleName() + " positionTransaction";
            Query query = entityManager.createQuery( jpqlString );
            return query.getResultList();
        }
        finally {
            if (entityManager != null) {
                entityManager.close();
            }
        }
	}
	/**
	 * Make the given instance managed and persistent.
	 */
	public void persistPositionTransaction(PositionTransaction positionTransaction) {
		entityManager.persist(entityManager.merge(positionTransaction));
	}
	/**
	 * Remove the given persistent instance.
	 */
	public void removePositionTransaction(PositionTransaction positionTransaction) {
		/*In JPA, objects detach automatically when they are serialized or when a persistence context ends.
		 The merge method returns a managed copy of the given detached entity.*/
		entityManager.remove(entityManager.merge(positionTransaction));
	}
}